//
//  ChooseTireViewController.m
//  大轮胎
//
//  Created by 朱永超 on 2017/3/16.
//  Copyright © 2017年 zyc. All rights reserved.
//

#import "ChooseTireViewController.h"
#import "YCNetWork.h"
#import "Model.h"
#import "LoadData.h"
#import "LeftTableViewCell.h"
#import "RightCollectionViewCell.h"
#import "ShoppingCartViewController.h"
#import <SDAutoLayout.h>

#import <SDWebImage/UIImageView+WebCache.h>

#define  US [UIScreen mainScreen].bounds
@interface ChooseTireViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    UITableView * tab;
    NSArray * arr;
    NSArray * arr1;
    UICollectionView * collect;
}
@property(nonatomic,strong)NSString * idsStr;

@end


@implementation ChooseTireViewController
-(void)viewWillAppear:(BOOL)animated
{
     self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    [[LoadData shardData]getMessage];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getM:) name:@"a" object:nil];
    [self tabbar]; //导航
    [self tab];
    [self collect];
   
}
-(void)getM:(NSNotification*)sender
{
    arr = sender.object;
    NSLog(@"123123%@",arr);
    [tab reloadData];
    NSIndexPath * selIndex = [NSIndexPath indexPathForRow:0 inSection:0];
    [tab selectRowAtIndexPath:selIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
    NSIndexPath * path = [NSIndexPath indexPathForItem:1 inSection:0];
    [self tableView:tab didSelectRowAtIndexPath:path];
}
-(void)tab
{
    tab =  [[UITableView alloc]init];
    tab.sd_layout.topEqualToView(self.view)
    .leftEqualToView(self.view)
    .widthIs(self.view.frame.size.width/3)
    .heightIs(self.view.frame.size.height);
    tab.delegate = self;
    tab.dataSource = self;
    [tab registerNib:[UINib nibWithNibName:@"LeftTableViewCell" bundle:nil] forCellReuseIdentifier:@"LeftTableViewCell"];
    [self.view addSubview:tab];
}
-(void)tabbar
{
    //设置标题
    self.title = @"轮胎商城";
    
    //设置返回时的标题
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    //设置导航标题字体大小和颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17], NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //设置背景颜色
    self.view.backgroundColor = [UIColor yellowColor];
}

#pragma UITabbleViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  arr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * strCell = @"LeftTableViewCell";
    LeftTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strCell];
    if (!cell)
    {
        cell = [[LeftTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCell];
    }
    Model * m = arr[indexPath.row];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.img]];
    cell.name.text = m.name;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // [tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中
    
    Model * mo = arr[indexPath.row];
    [[LoadData shardData]getMessageOne:mo.ids];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getMess:) name:@"b" object:nil];
    
    
}
-(void)getMess:(NSNotification*)sender
{
    arr1 = sender.object;
    NSLog(@"123124123124%@",arr1);
    [collect reloadData];
}
-(void)collect
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(US.size.width/3-10, (US.size.height-64-44)/3);
    //layout.minimumLineSpacing = 5;
    //layout.minimumInteritemSpacing = 5;
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5);
    
    
    
    collect = [[UICollectionView alloc]initWithFrame:CGRectMake(US.size.width/3, 64, US.size.width*2/3, US.size.height-44) collectionViewLayout:layout];
    
    
    collect.delegate = self;
    collect.dataSource = self;
    collect.backgroundColor = [UIColor colorWithRed:(243/255.0) green:(245/255.0) blue:(247/255.0) alpha:1];
    [collect registerNib:[UINib nibWithNibName:@"RightCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"RightCollectionViewCell"];
    [self.view addSubview:collect];
}
#pragma UICollectionViewDelegate
//返回分区个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//返回每个分区的item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return arr1.count;
}
//返回每个item
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Model_One * mo = arr1[indexPath.row];
    RightCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RightCollectionViewCell" forIndexPath:indexPath];
    //cell.backgroundColor = [UIColor yellowColor];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:mo.img]];
    cell.name.text = mo.name;
    cell.price.text = [NSString stringWithFormat:@"¥%@",mo.price];
    cell.priceNew.text = [NSString stringWithFormat:@"价格 ¥%@",mo.price_yj];
    //cell.renqi.text = [NSString stringWithFormat:@"人气 %@",mo.renqi];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Model_One * mo = arr1[indexPath.row];
    ShoppingCartViewController * shop = [[ShoppingCartViewController alloc]init];
    shop.ids = mo.ids;
    shop.name = mo.name;
    shop.price = mo.price;
    shop.newprice = mo.price_yj;
    shop.renqi = mo.renqi;
    [self.navigationController pushViewController:shop animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
